// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <iosfwd>

#include <nebula/testing/ktest_util.h>
#include <turbo/functional/iterator.h>

namespace nebula {

struct TestInt {
  TestInt();
  TestInt(int i);  // NOLINT runtime/explicit
  int value;

  bool operator==(const TestInt& other) const;

  friend std::ostream& operator<<(std::ostream& os, const TestInt& v);
};

struct TestStr {
  TestStr();
  TestStr(const std::string& s);  // NOLINT runtime/explicit
  TestStr(const char* s);         // NOLINT runtime/explicit
  explicit TestStr(const TestInt& test_int);
  std::string value;

  bool operator==(const TestStr& other) const;

  friend std::ostream& operator<<(std::ostream& os, const TestStr& v);
};

std::vector<TestInt> RangeVector(unsigned int max, unsigned int step = 1);

template <typename T>
inline turbo::Iterator<T> VectorIt(std::vector<T> v) {
  return turbo::make_vector_iterator<T>(std::move(v));
}

template <typename T>
inline turbo::Iterator<T> PossiblySlowVectorIt(std::vector<T> v, bool slow = false) {
  auto iterator = turbo::make_vector_iterator<T>(std::move(v));
  if (slow) {
    return turbo::make_transformed_iterator<T, T>(std::move(iterator),
                                         [](T item) -> turbo::Result<turbo::TransformFlow<T>> {
                                           SleepABit();
                                           return turbo::TransformYield(item);
                                         });
  } else {
    return iterator;
  }
}

template <typename T>
inline void AssertIteratorExhausted(turbo::Iterator<T>& it) {
  ASSERT_OK_AND_ASSIGN(T next, it.next());
  ASSERT_TRUE(turbo::is_iteration_end(next));
}

turbo::Transformer<TestInt, TestStr> MakeFilter(std::function<bool(TestInt&)> filter);

}  // namespace nebula

namespace turbo {
    template <>
    struct IterationTraits<nebula::TestInt> {
        static nebula::TestInt end() { return nebula::TestInt(); }
        static bool is_end(const nebula::TestInt& val) { return val == turbo::IterationTraits<nebula::TestInt>::end(); }
    };

    template <>
    struct IterationTraits<nebula::TestStr> {
        static nebula::TestStr end() { return nebula::TestStr(); }
        static bool is_end(const nebula::TestStr& val) { return val == turbo::IterationTraits<nebula::TestStr>::end(); }
    };

}